$(function () {
    //**************************************加载列表轮播图
    var layer = layui.layer

    function sw_show(params) {
        $.ajax({
            url: '/admin/swipers',
            success: function (res) {
                let str = ''

                $.each(res.data, function (index, item) {
                    imgsrc = "http://127.0.0.1:8888/uploads/" + item.swiperimg
                    if (item.swiperstatus == 1) {
                        str += `<tr _id=${item.id} _status=1>
                    <td>${item.id}</td>
                    <td>
                      <img src=${imgsrc}>
                    </td>
                    <td>/aaa/bb/cc/1.jpg</td>
                    <td>
                      <!-- 在使用 -->
                      <span class="layui-badge layui-bg-green">√</span>
                    </td>
                    <td>
                      <button type="button" class="layui-btn layui-btn-xs layui-btn-danger delete" _id=${item.id}>
                    删除
                  </button>
                    </td>
                  </tr>`
                    } else if (item.swiperstatus == 2) {
                        str +=
                            `<tr _id=${item.id} _status=2>
                       <td>${item.id}</td>
                       <td>
                         <img src="../assets/images/1.jpg">
                       </td>
                       <td>/aaa/bb/cc/1.jpg</td>
                       <td>
                         <!-- 未使用 -->
                         <span class="layui-badge layui-bg-cyan">×</span>
                       </td>
                       <td>
                         <button type="button" class="layui-btn layui-btn-xs layui-btn-danger delete" _id=${item.id}>
                       删除
                     </button>
                       </td>
                     </tr>`
                    }
                })
                $('tbody').html(str)
            }
        })
    }
    sw_show()




    // *************************************轮播图状态切换  使用还是未使用
    $('tbody').on('dblclick', 'tr', function () {
        let swiperstatus = $(this).attr('_status')
        let status = swiperstatus == 1 ? 2 : 1
        let id = $(this).attr('_id')
        $.ajax({
            type: 'PUT',
            url: `/admin/swipers/${id}`,
            data: {
                status: status
            },
            success: function (res) {
                layer.msg(res.message)
                sw_show()
            }
        })
    })







    // ***************************************删除操作
    $('tbody').on('click', '.delete', function () {
        let id = $(this).attr('_id')
        $.ajax({
            type: 'DELETE',
            url: `/admin/swipers/${id}`,
            success: function (res) {
                layer.msg(res.message)
                sw_show()
            }
        })
    })







    // ***************************************上传文件
    $('body').on('click', '#uploadSwiper', function () {
        $('#myfile').click()

    })

    // 监听文件选中事件
    $('body').on('change', '#myfile', function (e) {
        let files = e.target.files
        var fd = new FormData()
        $.each(files, function (index, item) {
            fd.append('swipers', item)
        })
        $.ajax({
            /* headers: {
                "Authorization": localStorage.getItem("mytoken") //此处放置请求到的用户token
            }, */
            type: 'post',
            url: '/admin/swipers',
            data: fd,
            processData: false,
            contentType: false, //
            success: function (res) {
                console.log(res.message);
                sw_show()
            }
        })


    })
})